package com.ciaojian.client.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ciaojian.core.enums.business.CouponCodeOriginEnum;
import com.ciaojian.core.mapper.MemberDetailMapper;
import com.ciaojian.core.model.*;
import com.ciaojian.core.service.MemberDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author Atlakyn
 */
@Service
public class MemberDetailServiceImpl extends MemberDetailService<MemberDetailMapper, MemberDetail> implements IService<MemberDetail> {
    @Resource
    private CouponCodeServiceImpl couponCodeService;
    @Resource
    private CouponServiceImpl couponService;

    /**
     * 发放券码
     *
     * @param userId   发放用户
     * @param optional MemberDetail 的 optional 包装
     */
    @Transactional(rollbackFor = Exception.class)
    public void distributeCode(
            Integer userId,
            Optional<MemberDetail> optional,
            CouponCodeOriginEnum origin
    ) {
        distributeCode(userId, optional, null, origin);
    }

    /**
     * 发放券码
     *
     * @param userId   发放用户
     * @param optional MemberDetail 的 optional 包装
     * @param order    权益订单
     */
    @Transactional(rollbackFor = Exception.class)
    public void distributeCode(Integer userId,
                               Optional<MemberDetail> optional,
                               Order order,
                               CouponCodeOriginEnum origin) {
        // coupon不为空表示需要发放券码
        optional.map(MemberDetail::getCoupon).ifPresent(bool -> {
            MemberDetail memberDetail = optional.get();
            // 给用户发放优惠券
            // 如果是券包
            if (bool) {
                couponCodeService.giveCouponCodeByMemberDetail(
                        userId,
                        order,
                        memberDetail,
                        origin);
            }
            // 如果是单张券
            else {
                CouponCode stock = couponCodeService.giveSingleCouponCode(
                        memberDetail.getCouponId(),
                        userId,
                        couponService.getCouponExpiredTime(memberDetail.getCouponId()),
                        origin);
                stock.setOrderId(order.getId());
                // 插入券码权益关联记录
                new CouponCodeMemberDetail(stock.getId(), memberDetail.getId()).insert();
                // 新增领取记录
                CouponCodeTakeRecord couponCodeTakeRecord = new CouponCodeTakeRecord();
                couponCodeTakeRecord.setUserId(userId);
                if (order != null && order.getWorkerId() != null) {
                    couponCodeTakeRecord.setWorkerId(order.getWorkerId());
                }
                if (order != null && order.getStoreId() != null) {
                    couponCodeTakeRecord.setStoreId(order.getStoreId());
                }
                couponCodeTakeRecord.setOrigin(origin);
                couponCodeTakeRecord.setCouponId(memberDetail.getCouponId());
                couponCodeTakeRecord.setCouponCode(stock.getCode());
                couponCodeTakeRecord.insert();
            }
        });
    }


    public List<MemberDetail> getMemberDetailList(int combinesId,int member){
        return this.list(new QueryWrapper<MemberDetail>().eq("combine_id",combinesId).eq("member",member));
    }
}
